import 'package:built_collection/built_collection.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/presentation/helpers/screen_util_mixin.dart';
import 'package:mobile_app.home.core/core/presentation/helpers/auto_scalable_text_mixin.dart';
import 'package:mobile_app.home.core/core/presentation/theme/home_core_theme.dart';
import 'package:mobile_app.home.core/core/presentation/widgets/card_button.dart';

class CardButtonInfo {
  const CardButtonInfo({
    required this.title,
    required this.onPressed,
  });

  final String title;
  final VoidCallback onPressed;
}

class CardButtonView extends StatelessWidget with ScreenUtilMixin {
  const CardButtonView({
    required this.cardButtonInfos,
    super.key,
  })  : assert(cardButtonInfos.length > 0);

  final List<CardButtonInfo> cardButtonInfos;

  double get _scaleFactor => logWidthScaleFactor;

  @override
  Widget build(BuildContext context) {
    final buttonGap = 8 * _scaleFactor;
    final dividedButtons = <Widget>[];

    for (var i = 0; i < cardButtonInfos.length; i++) {
      if (i > 0) {
        dividedButtons.add(SizedBox(
          width: buttonGap,
        ));
      }

      final cardButtonInfo = cardButtonInfos[i];

      dividedButtons.add(
        Expanded(
          child: _CardButton(
            title: cardButtonInfo.title,
            onPressed: cardButtonInfo.onPressed,
            autoSizableButtonTitles: cardButtonInfos.map((cardButtonInfo) {
              return cardButtonInfo.title;
            }).toBuiltSet(),
          ),
        ),
      );
    }

    return Padding(
      padding: EdgeInsets.all(8 * _scaleFactor),
      child: ConstrainedBox(
        constraints: const BoxConstraints(
          maxWidth: 500,
        ),
        child: Row(
          children: dividedButtons,
        ),
      ),
    );
  }
}

class _CardButton extends StatelessWidget with AutoScalableTextMixin, ScreenUtilMixin {
  const _CardButton({
    required this.title,
    required this.onPressed,
    required this.autoSizableButtonTitles,
  });

  final String title;
  final VoidCallback onPressed;
  final BuiltSet<String> autoSizableButtonTitles;

  double get _scaleFactor => logWidthScaleFactor;

  @override
  Widget build(BuildContext context) {
    return CardButton(
      onPressed: onPressed,
      child: Padding(
        padding: EdgeInsets.symmetric(
          vertical: 8 * _scaleFactor,
          horizontal: 14 * _scaleFactor,
        ),
        child: LayoutBuilder(
          builder: (context, constraints) {
            var minScaledFontSize = double.maxFinite;

            for (final autoSizableButtonTitle in autoSizableButtonTitles) {
              final scaledFontSize = calculateFontSize(
                text: autoSizableButtonTitle,
                constraints: constraints,
                style: HomeCoreTheme.of(context).textTheme.cardButton,
                maxLines: 2,
                textScaleFactor: _scaleFactor,
                stepGranularity: 0.1,
              );

              if (minScaledFontSize > scaledFontSize) {
                minScaledFontSize = scaledFontSize;
              }
            }

            final scaledTextStyle = HomeCoreTheme.of(context).textTheme.cardButton.copyWith(
                  fontSize: minScaledFontSize,
                );

            return Text(
              title,
              style: scaledTextStyle,
              textAlign: TextAlign.center,
              maxLines: 2,
            );
          },
        ),
      ),
    );
  }
}
